//
//  UIViewCreate.swift
//  CommonTools
//
//  Created by xianquan on 2023/12/8.
//

import Foundation
import SnapKit
import YYText
import TYUIKit

public protocol XWFontProvide {
    var font: UIFont {get}
    var pointSize: CGFloat {get}
    var regular: UIFont {get}
    var medium: UIFont {get}
    var semibold: UIFont {get}
    var bold: UIFont {get}
    var heavy: UIFont {get}
    var black: UIFont {get}

}
public extension XWFontProvide{
    
    var font: UIFont {
        if let f = self as? UIFont {
            return f
        }
        return pointSize.regular
    }
    
    func fontWeight(_ weight: UIFont.Weight) -> UIFont {
        UIFont.systemFont(ofSize: pointSize, weight: weight)
    }
    
    var regular: UIFont{
        fontWeight(.regular)
    }
    var medium: UIFont{
        fontWeight(.medium)
    }
    var semibold: UIFont{
        fontWeight(.semibold)
    }
    var bold: UIFont{
        fontWeight(.bold)
    }
    var heavy: UIFont{
        fontWeight(.heavy)
    }
    var black: UIFont{
        fontWeight(.black)
    }
}
extension UIFont: XWFontProvide{

}

extension CGFloat: XWFontProvide{

    public var pointSize: CGFloat{
        self
    }
}
extension Float: XWFontProvide{

    public var pointSize: CGFloat{
        CGFloat(self)
    }
}

extension Double: XWFontProvide{

    public var pointSize: CGFloat{
        CGFloat(self)
    }
}

extension Int: XWFontProvide{

    public var pointSize: CGFloat{
        CGFloat(self)
    }
}

public extension UIView {
    
    @discardableResult convenience init(backgroundColor: UIColor?, in view: UIView?, frame: CGRect? = nil, _ closure: ((_ make: ConstraintMaker) -> Void)? = nil) {
        self.init(frame: frame ?? .zero)
        self.backgroundColor = backgroundColor
        if let view = view {
            view.addSubview(self)
        }
        if let closure = closure {
            snp.makeConstraints(closure)
        }
    }
}

public extension UILabel {
    
    @discardableResult convenience  init(font: XWFontProvide?, textColor: UIColor?, in view: UIView?, frame: CGRect? = nil, _ closure: ((_ make: ConstraintMaker) ->Void)? = nil) {
        self.init(frame: frame ?? .zero)
        self.font = font?.font
        self.textColor = textColor
        if let view = view {
            view.addSubview(self)
        }
        if let closure = closure {
            snp.makeConstraints(closure)
        }
    }
}

public extension YYLabel {
    @discardableResult convenience  init(font: XWFontProvide?, textColor: UIColor?, in view: UIView?, frame: CGRect? = nil, _ closure: ((_ make: ConstraintMaker) ->Void)? = nil) {
        self.init(frame: frame ?? .zero)
        self.font = font?.font
        self.textColor = textColor
        if let view = view {
            view.addSubview(self)
        }
        if let closure = closure {
            snp.makeConstraints(closure)
        }
    }
}

public extension UIImageView {
    @discardableResult convenience init(imageInBundle: UIImage.NameInBundle?, originSize: CGSize? = nil, in view: UIView?, frame: CGRect? = nil, _ closure: ((_ make: ConstraintMaker) ->Void)? = nil){
        let image: UIImage? = UIImage.imageIn(bundleName: imageInBundle ?? .commonTools(""), originSize: originSize)
        self.init(image: image, in: view, frame: frame, closure)
    }
    
    @discardableResult convenience init(image: UIImage?, in view: UIView?, frame: CGRect? = nil, _ closure: ((_ make: ConstraintMaker) ->Void)? = nil){
        self.init(frame: frame ?? .zero)
        self.image = image
        if let view = view {
            view.addSubview(self)
        }
        if let closure = closure {
            snp.makeConstraints(closure)
        }
        
    }
}

public extension UIButton {
    @discardableResult convenience  init(title: String?, titleColor: UIColor?, imageInBundle: UIImage.NameInBundle?, originSize: CGSize? = nil, font: XWFontProvide?, in view: UIView?, frame: CGRect? = nil, _ closure: ((_ make: ConstraintMaker) ->Void)? = nil) {
        let image: UIImage? = UIImage.imageIn(bundleName: imageInBundle ?? .commonTools(""), originSize: originSize)
        self.init(image: image, title: title, titleColor: titleColor, font: font, in: view, frame: frame, closure)
        
    }

    @discardableResult convenience  init(image: UIImage?,title: String?, titleColor: UIColor?,  font: XWFontProvide?, in view: UIView?, frame: CGRect? = nil, _ closure: ((_ make: ConstraintMaker) ->Void)? = nil) {
        self.init(frame: frame ?? .zero)
        setTitle(title, for: .normal)
        setImage(image, for: .normal)
        if let view = view {
            view.addSubview(self)
        }
        setTitleColor(titleColor, for: .normal)
        titleLabel?.font = font?.font
        
        if let closure = closure {
            snp.makeConstraints(closure)
        }
    }
}

public extension UITextField {
    @discardableResult convenience init(font: XWFontProvide?, textColor: UIColor?, text: String, placeholder: String, pattern: String.Pattern = .none, maxLength: Int, in view: UIView?, frame: CGRect? = nil, _ closure: ((_ make: ConstraintMaker) ->Void)? = nil, _ textChange: ((_ text: String) ->Void)? = nil){
        
        self.init(frame: frame ?? .zero)
        if let view = view {
            view.addSubview(self)
        }
        self.text = text
        self.textColor = textColor
        self.font = font?.font
        self.pattern = pattern
        let att = NSMutableAttributedString(string: placeholder)
        att.yy_font = font?.font
        att.yy_color = .blackb0
        self.attributedPlaceholder = att
        clearButtonMode = .whileEditing
        if let closure = closure {
            snp.makeConstraints(closure)
        }
        
        addBlock(for: .editingChanged) { sender in
            guard let tf = sender as? UITextField else {return}
            tf.limitInput(type: tf.pattern, limitCount: maxLength)
            guard let textChange = textChange else {
                return
            }
            if let selectedRange = tf.markedTextRange {
                let markedTextLength = tf.offset(from: selectedRange.start, to: selectedRange.end)
                if markedTextLength > 0 {return}
                
                textChange(tf.text ?? "")
            }
            else {
                textChange(tf.text ?? "")
            }
            return
        }
    }
}
